//
//  MTimeDatePicker.swift
//  SwiftStudy3.0
//
//  Created by maliang on 2016/11/23.
//  Copyright © 2016年 maliang. All rights reserved.
//

import UIKit

class MTimeDatePicker: UIView,UIScrollViewDelegate {

    var rowHeight:CGFloat = 0.0
    var scrollView1:UIScrollView?
    var scrollView2:UIScrollView?
    var arr1 : [String]?
    var arr2 : [String]?
    var viewArr1 = [UILabel]()
    var viewArr2 = [UILabel]()
    var scrollView:UIScrollView{
    
        let scrollView = UIScrollView()
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.height = self.height
        scrollView.width = self.width / 2
        scrollView.delegate = self
        return scrollView
    }
    var itemLabel:UILabel{
    
        let label = UILabel()
        label.width = self.width / 2
        label.height = self.rowHeight
        label.textAlignment = NSTextAlignment.center
        return label
    }
    
    var lineView:UIView{
    
        let lineView = UIView.init(frame: self.bounds)
        lineView.height = self.rowHeight
        lineView.centerY = self.height / 2
        let topline = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.width, height: 0.5))
        topline.isUserInteractionEnabled = false
        topline.backgroundColor = UIColor.black
        let bottomline = UIView.init(frame: CGRect.init(x: 0, y: lineView.height, width: self.width, height: 0.5))
        bottomline.isUserInteractionEnabled = false
        bottomline.backgroundColor = UIColor.black
        lineView.addSubview(topline)
        lineView.addSubview(bottomline)
        lineView.isUserInteractionEnabled = false
        return lineView
    }
    
    /// 初始化方法
    ///
    /// - parameter frame:     frame(高度用 rowHeight * 行数 行数要为奇数)
    /// - parameter rowHeight: 行高
    ///
    /// - returns: MTimeDatePicker
    class func mTimePickerWithFrame(frame:CGRect, rowHeight:CGFloat) -> (MTimeDatePicker){
    
        let timePicker = MTimeDatePicker.init(frame: frame)
        timePicker.rowHeight = rowHeight
        timePicker.setUpView()
        return timePicker
    }
    
    func scrollViewWithSection(section:Int) -> UIScrollView {
        
        let scrollView = self.scrollView
        var arr = [String]()
        for idx in 1...24{
            arr.append(String.init(format: "%.2d点", idx))
            arr1 = arr
        }
        if section == 1 {
            scrollView.left = self.width / 2
            arr = Array.init(arrayLiteral: "0分","15分","30分","45分")
            arr2 = arr
        }
        let addRows =  Int(self.height / self.rowHeight) - 1
        let contentH =  self.rowHeight * CGFloat(arr.count + addRows)
        scrollView.contentSize = CGSize.init(width: 0, height: contentH)
        for (idx,obj) in arr.enumerated(){
        
            let label = self.itemLabel
            label.text = obj
            label.top = CGFloat(idx + addRows / 2) * self.rowHeight
            scrollView.addSubview(label)
            if  section == 0{
                
                viewArr1.append(label)
            }
            else{
            
                viewArr2.append(label)
            }
        }
        return scrollView
    }
    
    func setUpView() -> Void {
        
        self.scrollView1 = self.scrollViewWithSection(section: 0)
        self.addSubview(self.scrollView1!)
        self.scrollView2 = self.scrollViewWithSection(section: 1)
        self.addSubview(self.scrollView2!)
        self.addSubview(self.lineView)
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        self.fitContentOffSet(scrollView: scrollView)
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        
        if decelerate == false {
            
            self.fitContentOffSet(scrollView: scrollView)
        }
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        print(String.init(format: "%.0f", scrollView.contentOffset.y))
    }
    
    func fitContentOffSet(scrollView:UIScrollView){
        
        let contentOffSetY = scrollView.contentOffset.y
        var scrollToRow = Int(contentOffSetY) / Int(self.rowHeight)
        let moreMove = Int(contentOffSetY) % Int(self.rowHeight)
        if moreMove > Int(self.rowHeight / 2){
        
            scrollToRow += 1
        }
        var section = 0
        if scrollView.isEqual(scrollView2) {
            
            section = 1
        }
        self.selectRow(row: scrollToRow, inSection: section)
    }
    
    func selectRow(row : Int, inSection : Int) {
        
        var viewArr = viewArr1
        var scrollView = scrollView1
        if  inSection == 1 {
            
            scrollView = scrollView2
            viewArr = viewArr2
        }
        scrollView?.setContentOffset(CGPoint.init(x: 0, y: CGFloat(row) * self.rowHeight), animated: true)
        if viewArr.count > row{
            
            for (idx,obj) in viewArr.enumerated(){
                
                if  idx == row{
                    
                    obj.textColor = UIColor.red
                }
                else{
                    
                    obj.textColor = UIColor.black
                }
            }
        }

    }
}
